const app = getApp(),
 { $resHost} = app.globalData,
_ = require('../../pages/Common/underscore.js')


Page({
  data: {
    goodsList: [
      {
        "id": "20821",
        "deposit": "0.00",
        "sort": "20821",
        "brand": "",
        "type": "0",
        "goodsIntegral": "0",
        "auditStatus": "1",
        "independentEntityName": "优装优品",
        "goodsCommonId": "17480",
        "payType": "0",
        "goodsType": "1",
        "goodsClick": "0",
        "goodsSalenum": "0",
        "goodsCollect": "0",
        "goodsView": "0",
        "evaluationCount": "0",
        "goodsState": "1",
        "goodsUpdowntime": "2024-08-22 15:50:45",
        "goodsAddtime": "1724313045",
        "goodsEdittime": "1735393861",
        "goodsMarketprice": "450.00",
        "isNeedPrice": "1",
        "goodsVat": "0",
        "isFreeShip": "0",
        "isTop": "0",
        "topTime": "0",
        "isDownload": false,
        "downloadAt": "0",
        "downloadGoodsId": "0",
        "isBuy": "1",
        "orgId": "33763",
        "independentEntityId": "21879",
        "employeeId": "90738",
        "wechatId": "272",
        "videoImageId": "0",
        "videoId": "0",
        "coverId": "3087155",
        "minValue": "380.00",
        "maxValue": "380.00",
        "batchConversion": "1",
        "deliveryType": "1",
        "belongVideoAccount": "0",
        "fakeSaleNums": "0",
        "enableSplicingPattern": "0",
        "limitUser": "",
        "quantityLimit": "0",
        "displaySalenum": "0",
        "goodsName": "【大自然地板】冬日银沙 多层实木复合地板",
        "goodsJingle": "",
        "goodsUnit": "",
        "wxAuditStatus": "0",
        "shortName": "",
        "mainPic": [
            {
                "url": "https://res2.xjuke.com/upload/images/20240822/6e776a3c06284adeaf0c434a45f1c572.jpg",
                "thumb": "https://res2.xjuke.com/upload/images/20240822/6e776a3c06284adeaf0c434a45f1c572.jpg?x-oss-process=image/resize,m_mfit,w_640",
                "materialId": "3066430"
            }
        ],
        "goodsPrice": "380.00",
        "goodsStock": "100"
    },
    {
      "id": "20818",
      "deposit": "0.00",
      "sort": "20818",
      "brand": "",
      "type": "0",
      "goodsIntegral": "0",
      "auditStatus": "1",
      "independentEntityName": "优装优品",
      "goodsCommonId": "17479",
      "payType": "0",
      "goodsType": "1",
      "goodsClick": "0",
      "goodsSalenum": "0",
      "goodsCollect": "0",
      "goodsView": "0",
      "evaluationCount": "0",
      "goodsState": "1",
      "goodsUpdowntime": "2024-08-22 15:47:25",
      "goodsAddtime": "1724312845",
      "goodsEdittime": "0",
      "goodsMarketprice": "450.00",
      "isNeedPrice": "1",
      "goodsVat": "0",
      "isFreeShip": "0",
      "isTop": "0",
      "topTime": "0",
      "isDownload": false,
      "downloadAt": "0",
      "downloadGoodsId": "0",
      "isBuy": "1",
      "orgId": "33763",
      "independentEntityId": "21879",
      "employeeId": "90738",
      "wechatId": "272",
      "videoImageId": "0",
      "videoId": "0",
      "coverId": "0",
      "minValue": "380.00",
      "maxValue": "380.00",
      "batchConversion": "1",
      "deliveryType": "1",
      "belongVideoAccount": "0",
      "fakeSaleNums": "0",
      "enableSplicingPattern": "0",
      "limitUser": "0",
      "quantityLimit": "0",
      "displaySalenum": "0",
      "goodsName": "【大自然地板】晴空空谷  多层实木复合地板",
      "goodsJingle": "",
      "goodsUnit": "",
      "wxAuditStatus": "0",
      "shortName": "",
      "mainPic": [
          {
              "url": "https://res2.xjuke.com/upload/images/20240822/b06519a9eca2425bb1ce3e4ec3a22187.jpg",
              "thumb": "https://res2.xjuke.com/upload/images/20240822/b06519a9eca2425bb1ce3e4ec3a22187.jpg?x-oss-process=image/resize,m_mfit,w_640",
              "materialId": "3066403"
          }
      ],
      "goodsPrice": "380.00",
      "goodsStock": "100"
  }
    ], // 商品列表数据
    searchKeyword: '', // 搜索关键词
    filteredGoodsList: [], // 用于存储过滤后的商品列表
  },

  onLoad() {
    console.log(this.data.goodsList[0].mainPic[0].url);
    // 初始化时显示所有商品
    this.setData({
      filteredGoodsList: this.data.goodsList,
    });
    // 确保页面实例有 getOpenerEventChannel 方法
    if (typeof this.getOpenerEventChannel !== 'function') {
      this.getOpenerEventChannel = function() {
        return this.getOpenerEventChannel ? this.getOpenerEventChannel() : null;
      };
    }
    // this.getGoodsList(); // 页面加载时获取商品列表
  },

  // 获取商品列表
  // getGoodsList() {
  //   const that = this;
  //   xhs.request({
  //     url: 'https://wx2.xjuke.com/user/goodsclass/getlist', // 替换为实际的 API 地址
  //     method: 'GET',
  //     success(res) {
  //       if (res.statusCode === 200) {
  //         // 处理图片，确保为正方形
  //         const goodsList = res.data.map(item => {
  //           item.image = item.image + '?x-oss-process=image/resize,m_fill,h_200,w_200'; // 强制图片为 200x200
  //           return item;
  //         });
  //         console.log('获取的商品列表数据:', goodsList);
  //         that.setData({
  //           goodsList,
  //         });
  //       } else {
  //         console.error('请求失败:', res);
  //       }
  //     },
  //     fail(err) {
  //       console.error('请求出错:', err);
  //     },
  //   });
  // },

  // 搜索输入框内容变化
  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value,
    });
  },

  // 搜索确认
  onSearchConfirm() {
    const keyword = this.data.searchKeyword.trim().toLowerCase(); // 获取关键词并转为小写
    if (keyword) {
      const filteredGoodsList = this.data.goodsList.filter(item =>
        item.goodsName.toLowerCase().includes(keyword) // 匹配标题
      );
      this.setData({
        filteredGoodsList,
      });
    } else {
      // 如果关键词为空，显示所有商品
      this.setData({
        filteredGoodsList: this.data.goodsList,
      });
    }
  },

  // 加入购物车
  addToCart(e) {
    const goodsId = e.currentTarget.dataset.id;
    xhs.showToast({
      title: '已加入购物车',
      icon: 'success',
    });
    console.log('加入购物车的商品ID:', goodsId);
  },

  // 跳转到商品详情页
  goToGoodsDetail(e) {
    const goodsId = e.currentTarget.dataset.id;
    xhs.navigateTo({
      url: `/pages/GoodsDetail/index?id=${goodsId}`,
    });
  },
}); 